package gonnectd

import (
	"github.com/cz-it/gonnectd/proto"

	"encoding/binary"
	"io"
)

type Response struct {
	w io.Writer
}

func (rep *Response) Write(cmd uint8, buf []byte) (num int, err error) {
	headBuf := make([]byte, 8)

	headBuf[0] = cmd
	headBuf[1] = proto.VERSION
	binary.BigEndian.PutUint16(headBuf[2:4], proto.MAGIC)
	headBuf[4] = proto.REVERSE
	headBuf[5] = proto.HEADLEN
	binary.BigEndian.PutUint16(headBuf[6:8], uint16(len(buf)))

	// write head first
	rep.w.Write(headBuf)

	// write body then
	rep.w.Write(buf)
	return len(buf), nil
}
